#!/usr/local/bin/python3
# -*- coding:utf8 -*-
# Author Aaron 
# 2017-04-15 13:59:36
# 共阳数码管    低电平亮起
import RPi.GPIO as GPIO
import time

from datetime import datetime

global point
point = -1
global off
off = -2

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
tube1 = 29
tube2 = 12
tube3 = 16
tube4 = 18

led1 = 32
led2 = 36
led3 = 38
led4 = 40
led5 = 37
led6 = 35
led7 = 33
led8 = 31

switch = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(switch, GPIO.IN)
GPIO.setup(tube1, GPIO.OUT)
GPIO.setup(tube2, GPIO.OUT)
GPIO.setup(tube3, GPIO.OUT)
GPIO.setup(tube4, GPIO.OUT)

GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(led3, GPIO.OUT)
GPIO.setup(led4, GPIO.OUT)
GPIO.setup(led5, GPIO.OUT)
GPIO.setup(led6, GPIO.OUT)
GPIO.setup(led7, GPIO.OUT)
GPIO.setup(led8, GPIO.OUT)


def excute(values):
    GPIO.output(led1, values[0])
    GPIO.output(led2, values[1])
    GPIO.output(led3, values[2])
    GPIO.output(led4, values[3])
    GPIO.output(led5, values[4])
    GPIO.output(led6, values[5])
    GPIO.output(led7, values[6])
    GPIO.output(led8, values[7])


def reset():
    print("reset")
    GPIO.output(tube1, GPIO.LOW)
    GPIO.output(tube2, GPIO.LOW)
    GPIO.output(tube3, GPIO.LOW)
    GPIO.output(tube4, GPIO.LOW)
    excute([1, 1, 1, 1, 1, 1, 1, 1])


def test():
    print("test")
    GPIO.output(tube1, GPIO.HIGH)
    GPIO.output(tube2, GPIO.HIGH)
    GPIO.output(tube3, GPIO.HIGH)
    GPIO.output(tube4, GPIO.HIGH)
    excute([0, 0, 0, 0, 0, 0, 0, 0])
    time.sleep(0.2)
    show0()
    time.sleep(0.2)
    show1()
    time.sleep(0.2)
    show2()
    time.sleep(0.2)
    show3()
    time.sleep(0.2)
    show4()
    time.sleep(0.2)
    show5()
    time.sleep(0.2)
    show6()
    time.sleep(0.2)
    show7()
    time.sleep(0.2)
    show8()
    time.sleep(0.2)
    show9()
    time.sleep(0.2)
    point()


def point():
    print("point")
    excute([1, 1, 1, 0, 1, 1, 1, 1])


def show0():
    excute([0, 0, 0, 1, 0, 0, 0, 1])


def show1():
    excute([1, 1, 0, 1, 0, 1, 1, 1])


def show2():
    excute([0, 0, 1, 1, 0, 0, 1, 0])


def show3():
    excute([1, 0, 0, 1, 0, 0, 1, 0])


def show4():
    excute([1, 1, 0, 1, 0, 1, 0, 0])


def show5():
    excute([1, 0, 0, 1, 1, 0, 0, 0])


def show6():
    excute([0, 0, 0, 1, 1, 0, 0, 0])


def show7():
    excute([1, 1, 0, 1, 0, 0, 1, 1])


def show8():
    excute([0, 0, 0, 1, 0, 0, 0, 0])


def show9():
    excute([1, 0, 0, 1, 0, 0, 0, 0])


def andPoint():
    GPIO.output(led4, GPIO.HIGH)


def showOne():
    GPIO.output(tube1, GPIO.HIGH)
    GPIO.output(tube2, GPIO.LOW)
    GPIO.output(tube3, GPIO.LOW)
    GPIO.output(tube4, GPIO.LOW)


def showTwo():
    GPIO.output(tube2, GPIO.HIGH)
    GPIO.output(tube1, GPIO.LOW)
    GPIO.output(tube3, GPIO.LOW)
    GPIO.output(tube4, GPIO.LOW)


def showThree():
    GPIO.output(tube3, GPIO.HIGH)
    GPIO.output(tube1, GPIO.LOW)
    GPIO.output(tube2, GPIO.LOW)
    GPIO.output(tube4, GPIO.LOW)


def showFour():
    GPIO.output(tube4, GPIO.HIGH)
    GPIO.output(tube1, GPIO.LOW)
    GPIO.output(tube2, GPIO.LOW)
    GPIO.output(tube3, GPIO.LOW)


def showLed(one, two, three, four):
    if one == off:
        GPIO.output(tube1, GPIO.LOW)
    else:
        showOne()
        showNum(one)
    if two == off:
        GPIO.output(tube2, GPIO.LOW)
    else:
        showTwo()
        showNum(two)

    if three == off:
        GPIO.output(tube3, GPIO.LOW)
    else:
        showThree()
        showNum(three)
    if four == off:
        GPIO.output(tube4, GPIO.LOW)
    else:
        showFour()
        showNum(four)


def showNum(num):
    if num == 0:
        show0()
    if num == 1:
        show1()
    if num == 2:
        show2()

    if num == 3:
        show3()

    if num == 4:
        show4()

    if num == 5:
        show5()

    if num == 6:
        show6()
    if num == 7:
        show7()

    if num == 8:
        show8()
    if num == 9:
        show9()
    if num == point:
        point()


def showDay():
    currentTime = datetime.now()
    mon = int(currentTime.month / 10)
    mon1 = int(currentTime.month % 10)
    day = int(currentTime.day / 10)
    day1 = int(currentTime.day % 10)
    for i in range(0, 50, 1):
        # hour = int(currentTime.hour / 10)
        showLed(mon, off, off, off)
        time.sleep(0.005)
        # hour1 = int(currentTime.hour % 10)
        showLed(off, mon1, off, off)
        time.sleep(0.005)
        # min = int(currentTime.minute / 10)
        showLed(off, off, day, off)
        time.sleep(0.005)

        # min1 = int(currentTime.minute % 10)
        showLed(off, off, off, day1)
        time.sleep(0.005)


def showTime():
    # tz = pytz.timezone('Asia/Shanghai')
    # currentTime = datetime.now(tz)
    currentTime = datetime.now()
    hour = int(currentTime.hour / 10)
    showLed(hour, off, off, off)
    time.sleep(0.005)
    hour1 = int(currentTime.hour % 10)
    showLed(off, hour1, off, off)
    time.sleep(0.005)
    min = int(currentTime.minute / 10)
    showLed(off, off, min, off)
    time.sleep(0.005)

    min1 = int(currentTime.minute % 10)
    showLed(off, off, off, min1)
    time.sleep(0.005)


reset()
time.sleep(0.5)
test()

while True:
    if GPIO.input(switch) == GPIO.HIGH:
        showDay()
    showTime()

# time.sleep(50000000)
# showTime()
# showDay()
GPIO.cleanup()
